package com.glb.gilibuy.service.impl;

import com.glb.gilibuy.dao.UserOrderItemDao;
import com.glb.gilibuy.entity.*;
import com.glb.gilibuy.service.OrderItemService;
import com.glb.gilibuy.service.OrderService;
import com.glb.gilibuy.service.ProductService;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.util.QRCodeUtil;
import com.glb.gilibuy.util.SnowflakeIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/6 9:13
 */
@Service
public class OrderItemServiceImpl implements OrderItemService {
    @Autowired
    private UserOrderItemDao userOrderItemDao;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @Override
    public UserOrderItem selectItemByItemId(String itemId) {
        if (!"".equals(itemId)) {
            UserOrderItemExample userOrderItemExample = new UserOrderItemExample();
            userOrderItemExample.or().andOrderItemIdEqualTo(itemId).andIsDeletedEqualTo(0);
            List<UserOrderItem> userOrderItemList = userOrderItemDao.selectByExample(userOrderItemExample);
            if (userOrderItemList.isEmpty()) {
                return null;
            }
            return userOrderItemList.get(0);
        }
        return null;

    }

    @Override
    public List<UserOrderItem> selectItemsByOrderId(String orderId) {
        if (!"".equals(orderId)) {
            UserOrderItemExample userOrderItemExample = new UserOrderItemExample();
            userOrderItemExample.setOrderByClause("id desc");
            userOrderItemExample.or().andOrderIdEqualTo(orderId).andIsDeletedEqualTo(0);
            List<UserOrderItem> userOrderItemList = userOrderItemDao.selectByExample(userOrderItemExample);
            return userOrderItemList;
        }
        return null;
    }

    @Override
    public Map<String, Object> addOrderItem(String orderId, String productId, BigDecimal orderItemPrice, Integer orderItemAmount) {
        //该方法不直接面向外部，无权限验证，无参数校验
        UserOrderItem userOrderItem = new UserOrderItem();
        //生成订单详情项信息
        userOrderItem.setOrderId(orderId);
        userOrderItem.setProductId(productId);
        userOrderItem.setOrderItemPrice(orderItemPrice);
        userOrderItem.setOrderItemAmount(orderItemAmount);
        //生成订单商品id
        userOrderItem.setOrderItemId(String.valueOf(SnowflakeIdUtil.getSnowflakeId()));
        //订单创建时间
        userOrderItem.setGmtCreate(new Date());
        //激活
        userOrderItem.setIsDeleted(0);
        if (userOrderItemDao.insert(userOrderItem) > 0) {
            return MapGenerator.getRes(200, "订单商品项添加成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> createQrCode(String orderId) {
        //该方法不直接面向外部，无权限验证，无参数校验
        List<UserOrderItem> userOrderItemList = selectItemsByOrderId(orderId);
        if (userOrderItemList != null) {
            Boolean isVirtual = false;
            for (UserOrderItem userOrderItem : userOrderItemList) {
                Product product = productService.selectProductByProductId(userOrderItem.getProductId());
                //如果是虚拟商品生成二维码，否则忽视
                if (product.getIsVirtual() == 1) {
                    isVirtual = true;
                    Integer orderItemAmount = userOrderItem.getOrderItemAmount();
                    String qrCodeURL = "";
                    for (int i = 1; i <= orderItemAmount; i++) {
                        String picName = "qr_code_" + userOrderItem.getOrderItemId() + "_" + i + ".jpg";
                        try {
                            QRCodeUtil.encode(
                                    //二维码中的信息
                                    "{\"订单号\":\"" + userOrderItem.getOrderId() + "\","
                                            + "\"订单项号\":\"" + userOrderItem.getOrderItemId() + "\","
                                            + "\"票数\":\"" + orderItemAmount + "\","
                                            + "\"票数序列\":\"" + i + "\","
                                            + "\"虚拟商品兑换码\":\"" + QRCodeUtil.createCode() + "\"}",
                                    //二维码中间图片的位置(不认网址)
                                    "G:\\PP\\local\\file\\" + "logo.png",
                                    //输出二维码的位置
                                    "G:\\PP\\local\\file\\" + picName,
                                    true);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        qrCodeURL = qrCodeURL + "/file/" + picName + ",";
                    }
                    //截掉最后一位逗号
                    qrCodeURL = qrCodeURL.substring(0, qrCodeURL.length() - 1);
                    userOrderItem.setQrCode(qrCodeURL);
                    if (userOrderItemDao.updateByPrimaryKeySelective(userOrderItem) <= 0) {
                        return MapGenerator.getRes(520, "内部错误！");
                    }
                }
            }
            if (isVirtual) {
                //含有虚拟商品的订单，一旦生成二维码成功，直接变成交易完成状态
                UserOrder userOrder = new UserOrder();
                userOrder.setOrderState(3);
                userOrder.setOrderId(orderId);
                orderService.updateOrder(userOrder);
                return MapGenerator.getRes(200, "生成二维码成功！");
            } else {
                return MapGenerator.getRes(201, "不含有虚拟商品！");
            }
        }
        return MapGenerator.getRes(500, "订单不存在！");
    }
}